/*
 * GuiDisplaySearchResults.java
 *
 * Created on 24 July 2003, 07:58
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.interfaces.*;
import com.velocityme.utility.NodeSearchResult;
import com.velocityme.valueobjects.*;
import java.util.*;
import java.awt.Component;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.naming.NamingException;
import javax.swing.JTabbedPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author  Robert Crida Work
 */
public class GuiDisplaySearchResults extends javax.swing.JPanel {
    
    private NodePK m_searchRootPK;
    private KeySession m_key;
    private SearchTableModel m_modelSearch;
    private TableSorter m_tableSorter;
    
    /** Creates new form GuiDisplaySearchResults */
    public GuiDisplaySearchResults(KeySession p_key, NodePK p_searchRootPK, Collection p_results) {
        initComponents();
        m_key = p_key;
        m_searchRootPK = p_searchRootPK;
        jTabbedPane1.addTab("Report", new SearchReportPanel(p_key, p_searchRootPK, m_tableSorter));
        try {
            jTextFieldSearchRoot.setText(StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, m_searchRootPK));
            Iterator i = p_results.iterator();
            while (i.hasNext()) {
                m_modelSearch.addRow((NodeSearchResult)(i.next()));
            }
            jTabbedPane.addTab("View Node",null);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
        jTableSearch.getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableSearch.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evt) {
                try {
                    if (jTableSearch.getSelectedRow() >= 0) {
                        Integer nodeID = (Integer)m_tableSorter.getValueAt(jTableSearch.getSelectedRow(),0);
                        NodePK nodePK = new NodePK(nodeID);
                        Set setPermissions = StatelessRemoteClientSessionUtil.getSession().getAssignedPermissions(m_key, nodePK);
                        NodeValue nodeValue = StatelessRemoteClientSessionUtil.getSession().getNodeValue(m_key, nodePK);
                        jTabbedPane.setComponentAt(0,NodeDisplay.getDisplay(m_key,nodeValue,setPermissions));
                        jTabbedPane.setSelectedIndex(0);
                    }
                }
                catch(Exception e) {
                    ExceptionWindow.showError(e);
                }
            }
        });
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButtonClose = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPaneNodes = new javax.swing.JScrollPane();
        m_modelSearch = new SearchTableModel();
        m_tableSorter = new TableSorter(m_modelSearch);
        jTableSearch = new javax.swing.JTable(m_tableSorter);
        m_tableSorter.setTableHeader(jTableSearch.getTableHeader());
        jPanel3 = new javax.swing.JPanel();
        jLabelSearchRoot = new javax.swing.JLabel();
        jTextFieldSearchRoot = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTabbedPane = new javax.swing.JTabbedPane();

        setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonClose.setText("Close");
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonClose);

        jPanel4.add(jPanel2, java.awt.BorderLayout.SOUTH);

        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
        jScrollPaneNodes.setViewportView(jTableSearch);

        jPanel1.add(jScrollPaneNodes, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        jLabelSearchRoot.setText("Search Root:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel3.add(jLabelSearchRoot, gridBagConstraints);

        jTextFieldSearchRoot.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel3.add(jTextFieldSearchRoot, gridBagConstraints);

        jPanel1.add(jPanel3, java.awt.BorderLayout.NORTH);

        jSplitPane1.setLeftComponent(jPanel1);

        jScrollPane1.setViewportView(jTabbedPane);

        jSplitPane1.setRightComponent(jScrollPane1);

        jPanel4.add(jSplitPane1, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Browse", jPanel4);

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
        // Add your handling code here:
        getParent().remove(this);        
    }//GEN-LAST:event_jButtonCloseActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JLabel jLabelSearchRoot;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPaneNodes;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTableSearch;
    private javax.swing.JTextField jTextFieldSearchRoot;
    // End of variables declaration//GEN-END:variables
    
}
